package com.shenjun.bank.servers;

import com.shenjun.bank.handler.HeartbeatHandler;
import com.shenjun.bank.handler.HelloServerHandler;
import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;

@Component
public class HelloServer {

    @Autowired
    private HelloServerHandler helloServerHandler;

    @PostConstruct
    public void start() throws Exception {
        EventLoopGroup bossGroup = new NioEventLoopGroup(); // 用于接受客户端连接的线程组
        EventLoopGroup workerGroup = new NioEventLoopGroup(); // 用于处理网络事件的线程组

        ServerBootstrap b = new ServerBootstrap();
        b.group(bossGroup, workerGroup)
            .channel(NioServerSocketChannel.class) // 使用NIO传输Channel
            .childHandler(helloServerHandler);
        b.bind(8001).sync().channel().closeFuture().sync(); // 绑定端口并同步等待关闭Channel，启动服务器监听端口请求。
    }

}
